// 文件路径：app/src/main/java/com/example/qrcodecamera/CustomScannerActivity.java
package com.example.qrcodecamera;

import android.graphics.Rect;
import android.os.Bundle;
import com.journeyapps.barcodescanner.CaptureActivity;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;

public class CustomScannerActivity extends CaptureActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected DecoratedBarcodeView initializeContent() {
        DecoratedBarcodeView barcodeView = super.initializeContent();

        // 获取屏幕尺寸
        int screenWidth = getResources().getDisplayMetrics().widthPixels;
        int screenHeight = getResources().getDisplayMetrics().heightPixels;
        int frameSize = (int) (Math.min(screenWidth, screenHeight) * 0.8); // 扫描框大小为屏幕短边的80%

        // 计算居中位置
        Rect framingRect = new Rect(
                (screenWidth - frameSize) / 2,
                (screenHeight - frameSize) / 2,
                (screenWidth + frameSize) / 2,
                (screenHeight + frameSize) / 2
        );

        // 设置扫描框
        CustomViewfinderView viewfinderView = barcodeView.findViewById(R.id.zxing_viewfinder_view);
        if (viewfinderView != null) {
            viewfinderView.setFramingRect(framingRect);
        }

        return barcodeView;
    }
}